package de.rtbfaustball.converters;

import java.io.IOException;
import org.apache.pdfbox.exceptions.COSVisitorException;

public abstract class AbstractConverter {

	protected static final String DEFAULT_PERSISTANCE_LOCATION = "target";

	/**
	 * Convert collected data to HTML. Styles are inherited from www.rtbfaustball.de mostly. Generated markup is persisted as well.
	 */
	public void toHtml() throws IOException {
		toHtml(true);
	}

	/**
	 * Convert collected data to HTML. Styles are inherited from www.rtbfaustball.de mostly.
	 * 
	 * @param persist
	 *            Determins whether markup should be persisted or not.
	 */
	public abstract void toHtml(boolean persist) throws IOException;

	/**
	 * Write given markup to file.
	 * 
	 * @param markup
	 *            HTML markup to store.
	 */
	protected abstract void persistHtml(String markup) throws IOException;

	/**
	 * Convert collected data to PDF. Generated PDF is persisted as well.
	 */
	public void toPdf() throws IOException, COSVisitorException {
		toPdf(true);
	};

	/**
	 * Convert collected data to PDF. Generated PDF is persisted as well.
	 * 
	 * @param persist
	 *            Determins whether markup should be persisted or not.
	 */
	public abstract void toPdf(boolean persist) throws IOException, COSVisitorException;

	/**
	 * Convert collected data to Excel.
	 */
	public abstract void toExcel();

	// *************** HTML utility methods ************************************

	/**
	 * Escapes umlaut marks and special characters to ensure correct html representation.
	 * 
	 * @param html
	 * @return
	 */
	protected static String escapeHtml(final String html) {
		String ret = html;
		ret = ret.replaceAll("ä", "&auml;");
		ret = ret.replaceAll("ö", "&ouml;");
		ret = ret.replaceAll("ü", "&uuml;");
		ret = ret.replaceAll("Ä", "&Auml;");
		ret = ret.replaceAll("Ö", "&Ouml;");
		ret = ret.replaceAll("Ü", "&Uuml;");
		ret = ret.replaceAll("ß", "&szlig;");
		return ret;
	}

	/**
	 * Append given parameters to StringBuffer if value is not null.
	 * 
	 * @param reference
	 * @param value
	 */
	protected static void appendHtml(StringBuffer reference, String value) {
		appendHtml(reference, value, null, null);
	}

	/**
	 * Append given parameters to StringBuffer if value is not null.
	 * 
	 * @param reference
	 * @param value
	 * @param suffix
	 */
	protected static void appendHtml(StringBuffer reference, String value, String suffix) {
		appendHtml(reference, value, null, suffix);
	}

	/**
	 * Append given parameters to StringBuffer if value is not null.
	 * 
	 * @param reference
	 * @param value
	 * @param prefix
	 * @param suffix
	 */
	protected static void appendHtml(StringBuffer reference, String value, String prefix, String suffix) {
		if (value != null) {
			if (prefix != null) {
				reference.append(prefix);
			}
			reference.append(value);
			if (suffix != null) {
				reference.append(suffix);
			}
		}
	}

	// *************** PDF utility methods *************************************

	/**
	 * Append given parameters to StringBuffer if value is not null.
	 * 
	 * @param reference
	 * @param value
	 */
	protected static void appendPdf(StringBuffer reference, String value) {
		appendPdf(reference, value, null);
	}

	/**
	 * Append given parameters to StringBuffer if value is not null.
	 * 
	 * @param reference
	 * @param value
	 * @param prefix
	 */
	protected static void appendPdf(StringBuffer reference, String value, String prefix) {
		if (value != null) {
			if (prefix != null) {
				reference.append(prefix);
			}
			reference.append(value);
		}
	}

	// *************** Excel utility methods ***********************************

}
